%% this shows the mean PSNR,and iteration
clc; clear;
% close all;
data_path = '/home/wks/mywks/SR-Works/code/data';
result_path = '/home/wks/mywks/SR-Works/code/%result';

pre_net_model = '/home/wks/mywks/SR-Works/code/%result/reimple_VDSR-1/reimple_VDSR_deploy.prototxt';
pre_net_weights='/home/wks/mywks/SR-Works/code/%result/reimple_VDSR-1/models/reimple_VDSR_c1_clip1_more_iter_132500.caffemodel';

%%
scale = 3;
interval = 1000;

%%
% test_data = fullfile(data_path, 'Set14');
test_data = fullfile(data_path, 'Set5');
%  test_data = fullfile(data_path, 'mytest');
% test_data = fullfile(data_path, 'General100');
% test_data = fullfile(data_path, 'B100');

%% set caffemodel and deploy file path !!!!
net_folder = 'reimpleVDSR_y_lr0.01-1';
net_file = 'reimpleVDSR_y_lr0.01_deploy.prototxt';
snapshort_prefix = 'reimpleVDSR_y_usual_net3_iter_';

%%
model_path = fullfile(result_path, net_folder, 'models');
net_model =  fullfile(result_path, net_folder, net_file);

%% we should do some preparation,include our caffemodel
model_files = mycaffe.getModelFiles(model_path, snapshort_prefix, interval);
ModelNum = length(model_files);

%%
img_files = srimg.dirimgs(test_data);
ImgNum = length(img_files);

%filemodelpaths = dir(fullfile(caffepa,'*.caffemodel'));
psnr_bi = zeros(ImgNum, 1);
psnr_h1 = psnr_bi;
psnr_h2 = zeros(ImgNum, ModelNum);

phase = 'test';
mycaffe.init('gpu 0')
for i = 1 : ImgNum
    %% read ground truth image
    im0 = srimg.imread(fullfile(test_data, img_files(i).name), scale);
    
    %% bicubic interpolation
    im_bi = srimg.im2lr(im0, scale);
    psnr_bi(i) = srimg.psnr(im0, im_bi, scale);
    
    %% Precoditional
    im_h1 = mycaffe.caffe(im_bi, pre_net_model, pre_net_weights, phase);
    psnr_h1(i) = srimg.psnr(im0, im_h1, scale);
    
    %% reimpleVDSR_y
    for j = 1 : ModelNum
        fprintf('%d / %d\n', j, ModelNum);
        net_weights = fullfile(model_path, model_files{j});
        im_h2 = mycaffe.caffe(im_h1, net_model, net_weights, phase);
        psnr_h2(i, j) = srimg.psnr(im0, im_h2, scale);
    end
end

psnr_bi_bar = mean(psnr_bi);
psnr_h1_bar = mean(psnr_h1);
psnr_h2_bar = mean(psnr_h2);

%% Finally,we draw the relationship between iteration(caffemodel) and psnr
%figure;plot(psnr_iter_srcnn-clip,'r');hold on;plot(psnr_iter_bic,'--g');hold on;plot(psnr_iter_goal,'b');xlabel('srcnn-clip-iteration');ylabel('srcnn-clip-psnr');title('this my srcnn-clip');legend('psnr-iter-srcnn-clip','psnr-iter-bic','psnr-iter-goal');
figure;
len = length(psnr_h2_bar);  % count the real length
plot(psnr_h2_bar, 'r'); hold on;
plot(psnr_h1_bar(ones(1, len)), 'b:', 'linewidth', 2);
plot(psnr_bi_bar(ones(1, len)), 'g--');

xlabel('iteration'); ylabel('psnr');
legend('h2', 'h1', 'bic');

%% the following is to help us annalyse
[psnr_h2_bar, idx] = sort(psnr_h2_bar);
fprintf('\n\nThe following is our report\n');
fprintf('******************************\n');
fprintf('Bicubic: %f\n', psnr_bi_bar);
fprintf('MIN = %f, MAX =  %f, MEAN =  %f\n', psnr_h2_bar(1), psnr_h2_bar(end), mean(psnr_h2_bar))
fprintf('Caffemodel wit MAX PSNR is %d \n', model_files{idx(end)});
fprintf('******************************\n');

%% save
% save(saveMatName, psnr_h2);
